#!/user/bin/env python3
# -*- coding: utf-8 -*-
"""
--------------------------------------
    Author:     JiChao_Song
    Date  :     2020/12/18 11:48
    Desc  :
--------------------------------------
"""
import time

from flask import Blueprint, render_template, request
from application import app
from common.libs.AlibabaUtils import AlibabaUtils, get_filename
from common.libs.JsonResponseUtils import JsonResponse
from flask import Blueprint

file_router = Blueprint('file', __name__)


@file_router.route('/upload', methods = ['POST'])
def upload():
    try:
        file = request.files.get('file')
        if file:
            print(type(file))
            filename, ext = get_filename(file.filename)
            putobjectname = f'{filename}-{int(time.time())}{ext}'
            url = AlibabaUtils().uploadFile(file, putobjectname)
            return JsonResponse(code = 20000, data = {'url': url}, message = '请求成功').jsonExit()
        return JsonResponse(code = 40002, data = None, message = 'file不能为空').jsonExit()
    except Exception as e:
        app.logger.error(e)
        return JsonResponse(code = 50000, data = None, message = "系统异常", errors = f'{e}').jsonExit()
